﻿using UnityEngine;
using System.Collections;
namespace Pathfinding
{
    public class WayLinkObject : MonoBehaviour
    {
        public string linkID;
        public int StartPointID;
        public int EndPointID;
        public enum WayType 
        {
           Src2Dst,
           Dst2Src,
           TwoWay
        }

        public enum WayActionType 
        {
           Run,
           Jump,
           FailDown
        }

       
        public WayType LinkWayType = WayType.TwoWay;
        public WayPointRoot PointRoot;
        public Transform S2DLinkObject;
        public Transform D2SLinkObject;
        Transform S2DArrow;
        Transform D2SArrow;

        public WayActionType S2DActionType = WayActionType.Run;
        public WayActionType D2SActionType = WayActionType.Run;

        public void New(int start,int end,string id = "",WayActionType S2DActionType = WayActionType.Run,WayType wayType = WayType.TwoWay,WayActionType D2SActionType = WayActionType.Run) 
        {
            if (id == "")
            {
                this.linkID = System.Guid.NewGuid().ToString("N");
            }
            else 
            {
                this.linkID = id;
            }
            S2DLinkObject = transform.Find("s2dLine");
            S2DLinkObject.localScale = new Vector3(S2DLinkObject.transform.localScale.x * PointRoot.WayObjectScale,1, S2DLinkObject.transform.localScale.z * PointRoot.WayObjectScale);
            D2SLinkObject = transform.Find("d2sLine");
            this.D2SLinkObject.localScale = new Vector3(D2SLinkObject.transform.localScale.x*PointRoot.WayObjectScale,1,D2SLinkObject.transform.localScale.z*PointRoot.WayObjectScale);
            this.S2DArrow = transform.Find("s2darrow");
            this.D2SArrow = transform.Find("d2sarrow");
            this.S2DArrow.transform.localScale *= 0.3f;
            this.D2SArrow.transform.localScale *= 0.3f;
            this.StartPointID = start;
            this.EndPointID = end;
            this.LinkWayType = wayType;
            this.S2DActionType =S2DActionType;
            this.D2SActionType = D2SActionType;
        }

        public void Update() 
        {
            WayPointObject startPoint = PointRoot.GetWayPointById(this.StartPointID);
            WayPointObject endPoint = PointRoot.GetWayPointById(this.EndPointID);
            Vector3 subVec = endPoint.transform.position - startPoint.transform.position;
            float length = subVec.magnitude;
            System.Action<Transform,int,Transform> LinkTrans = (Transform trans,int offsetIndex,Transform arrow) =>
            {
                trans.localScale = new Vector3(trans.localScale.x,length*0.5f,trans.localScale.z);
                trans.position = startPoint.transform.position + (subVec.normalized * length * 0.5f);
               
                trans.LookAt(endPoint.transform);
               
                trans.Rotate(90,0,0);

                Vector3 dir = trans.up;
                Vector3 updir =   Quaternion.Euler(0, 0, offsetIndex * 90) * dir;
                updir = updir.normalized * 0.2f;
                trans.position += updir;
                arrow.transform.localPosition = trans.position;
                
                if (offsetIndex == 1)
                {
                    arrow.transform.LookAt(startPoint.transform);
                }
                else
                {
                    arrow.transform.LookAt(endPoint.transform);
                }
            };

            System.Action<WayActionType,Transform > SetWayTypeColor= (actionType, trans) =>
            {
                switch (actionType)
                {
                    case WayActionType.Jump:
                        trans.gameObject.GetComponent<MeshRenderer>().material.color = new Color(0,0,1,0.5f);
                        break;
                    case WayActionType.Run:
                        trans.gameObject.GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, 0.5f);
                        break;
                    case WayActionType.FailDown:
                        trans.gameObject.GetComponent<MeshRenderer>().material.color = Color.gray;
                        break;
                }
            };
            SetWayTypeColor(this.S2DActionType,this.S2DLinkObject);
            SetWayTypeColor(this.D2SActionType, this.D2SLinkObject);
            switch (this.LinkWayType)
            {
                case WayType.Src2Dst:
                    this.S2DLinkObject.gameObject.SetActive(true);
                    this.D2SLinkObject.gameObject.SetActive(false);
                    this.S2DArrow.gameObject.SetActive(true);
                    this.D2SArrow.gameObject.SetActive(false);
                    LinkTrans(this.S2DLinkObject,-1,this.S2DArrow);
                    break;
                case WayType.Dst2Src:
                    this.D2SLinkObject.gameObject.SetActive(true);
                    this.S2DLinkObject.gameObject.SetActive(false);
                    this.D2SArrow.gameObject.SetActive(true);
                    this.S2DArrow.gameObject.SetActive(false);
                    LinkTrans(this.D2SLinkObject,1,this.D2SArrow);
                    break;
                case WayType.TwoWay:
                    this.S2DLinkObject.gameObject.SetActive(true);
                    this.D2SLinkObject.gameObject.SetActive(true);
                    LinkTrans(this.S2DLinkObject,-1,this.S2DArrow);
                    LinkTrans(this.D2SLinkObject,1,this.D2SArrow);
                    this.S2DArrow.gameObject.SetActive(true);
                    this.D2SArrow.gameObject.SetActive(true);
                    break;
            }
        }
    }
}
